//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import BScroll from "better-scroll";
// import Swiper  from "Swiper";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
  return {
    winningArr:[],
    winningScroll: "",
    freeId:'',
    participant:[],
    lotteryList:[],
    endTime:'',
    presentTime:"",
    status:'',
    localStatus:false,
    nowFree:'',
    swiperOption: {
      //   loop模式第二轮循环的时候第一张会闪烁
      loop: true,
      delay: 3000,
      direction: "vertical",
      observer: true,
      observer:true,//修改swiper自己或子元素时，自动初始化swiper
      observeParents:false,//修改swiper的父元素时，自动初始化swiper
      // autoplay:true,
      autoplay: {
        delay: 2500,
        disableOnInteraction: false,
        stopOnLastSlide:false
      },
      on:{
        autoplay:function(){
        //your code
          // console.log('333333');
        },
        reachEnd:function(){
          // console.log('到了最后一个slide');
          // this.$refs.mySwiper.swiper.
          // this.$refs.mySwiper.swiper.autoplay.start();
        },
      },
      pagination: {
        el: ".swiper-pagination"
      },
      timeSet:'',
      imgPath:'',
      freeImg:''
    },
  };
  },
  //监听属性 类似于data概念
  computed: {
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    lotteryTo() {
      if(this.timeSet) {
        clearInterval(this.timeSet);
      }
      this.$router.push(this.fun.getUrl('baseRewardTemplateRecommendReward',{},{api:'plugin.free-order-return.api.index.index'}));
    },
    _initScrollFree() {
      if(!this.winningScroll && this.winningArr.length > 0){
        this.winningScroll = new BScroll(this.$refs.winningRef, {
          click: true,
          scrollY: true,
          bounce: {
            top: 22
          },
          probeType: 3,
          swipeBounceTime: 500,
          useTransition: false,
          mouseWheel: true
        });
      } else {
        this.winningScroll.refresh();
      }
    },
    getFreeId() {
      $http
        .post("plugin.free-order-return.api.index.get-latest-id ", {})
        .then(res => {
          if (res.result === 1) {
            let data = res.data;
            this.freeId = Number(data.id);
            this.freeDetail(this.freeId);
          } else {
            Toast(res.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    freeDetail(id) {
      $http
        .post("plugin.free-order-return.api.index.record-detail", {id: id,basic_info:1})
        .then(res => {
          if (res.result === 1) {
            let data = res.data;
            this.imgPath = res.basic_info.host;
            this.freeImg = `${this.imgPath}free-order-return/free-bg.png`;
           this.lotteryList = data.participant;
           this.winningArr = data.choosen;
           let date = new Date(data.end_time);
           this.endTime = date.getTime();
           this.status = data.status;
           this.freeData();
           if(data.status) {
              clearInterval(this.timeSet);
           }
           if(this.nowFree >= this.endTime && !data.status) {
            this.localStatus = true;
            if (this.timeSet == null) {
              this.timeSet = setInterval(() => {
                this.freeDetail(this.freeId);
              }, 3000);
            }
           } else {
            this.presentTime = this.endTime - this.nowFree;
            this.localStatus = false;
           }
          } else {
            Toast(res.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    freeData() {
      this.$nextTick(() => {
        this._initScrollFree();
      });
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  deactivated() {
    clearInterval(this.timeSet);
    this.timeSet = null;
    this.lotteryList = [];
  },
  activated() {
    this.nowFree = new Date().getTime();
    if(this.$route.query.id) {
      this.freeId = Number(this.$route.query.id);
      this.freeDetail(this.freeId);
    } else {
      this.getFreeId();
    }
    
   
  }, //如果页面有keep-alive缓存功能，这个函数会触发
  }